﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsListView;

namespace WindowsFormsListViewSort
{
    public partial class Form1 : Form
    {
        // 当前排序
        private bool asc = true;

        public Form1()
        {
            InitializeComponent();

            InitListView();

            // 列出所有项
            LoadDir(new DirectoryInfo("G:\\share_dir"));
        }

        private void InitListView()
        {
            // 设置显示模式
            listView1.View = View.Details;
            // 整行选中
            listView1.FullRowSelect = true;

            // 设置列名
            // 宽度值-2表示自动调整宽度
            listView1.Columns.Add("文件名", -2, HorizontalAlignment.Left);
            listView1.Columns.Add("修改时期", 150, HorizontalAlignment.Left);
            listView1.Columns.Add("类型", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("大小", -2, HorizontalAlignment.Left);

            // 设置小图标列表
            ImageList imageList = new ImageList();
            imageList.ImageSize = new Size(16, 16);
            imageList.Images.Add(Properties.Resources.Icon_File);
            imageList.Images.Add(Properties.Resources.Icon_Folder);
            // 设置 SmallImageList 用于显示
            listView1.SmallImageList = imageList;

            // 将图标添加到 SmallImageList，并关联一个 key 值，后面可以根据 key  值获取此图片
            imageList.Images.Add("Sort_ASC", Properties.Resources.up);
            imageList.Images.Add("Sort_DESC", Properties.Resources.down);
            listView1.Columns[0].ImageKey = "Sort_ASC";     // 将图标绑定到第一列


            // 设置大图标列表
            ImageList imageList2 = new ImageList();
            imageList2.ImageSize = new Size(64, 64);
            imageList2.Images.Add(Properties.Resources.Icon_File2);
            imageList2.Images.Add(Properties.Resources.Icon_Folder2);
            // 设置 SmallImageList 用于显示
            listView1.LargeImageList = imageList2;
        }

        private void LoadDir(DirectoryInfo dir)
        {
            // listView1.BeginUpdate();

            // 子目录
            DirectoryInfo[] subDirs = dir.GetDirectories();
            foreach (DirectoryInfo d in subDirs)
            {
                if ((d.Attributes & FileAttributes.Hidden) > 0) continue;

                MyListItemTag tag = new MyListItemTag();
                tag.path = d.FullName;
                tag.name = d.Name;
                tag.time = d.LastWriteTime;
                tag.type = 0;
                tag.size = -1;
                tag.ext = "文件夹";
                AddListItem(tag);
            }
            // 子文件
            FileInfo[] subFiles = dir.GetFiles();
            foreach (FileInfo f in subFiles)
            {
                if ((f.Attributes & FileAttributes.Hidden) > 0) continue;

                MyListItemTag tag = new MyListItemTag();
                tag.path = f.FullName;
                tag.name = f.Name;
                tag.time = f.LastWriteTime;
                tag.type = 1;
                tag.size = f.Length;
                tag.ext = f.Extension.ToUpper();
                AddListItem(tag);
            }

            // listView1.EndUpdate();
        }

        private void AddListItem(MyListItemTag tag)
        {
            // 判断是文件还是文件夹，使用不同的图标
            int imageIndex = 0;
            if (tag.type == 1) imageIndex = 1;

            ListViewItem item = new ListViewItem(tag.name, imageIndex);
            item.Tag = tag;

            // 时间
            item.SubItems.Add(tag.time.ToString("yyyy-MM-dd HH:mm"));
            // 类型
            item.SubItems.Add(tag.ext);
            // 文件大小
            long size = tag.size;
            string sizeStr = "";
            if (size < 0)
                sizeStr = ""; // 文件夹不显示大小
            else if (size < 1000)
                sizeStr = "" + size;
            else if (size < 1000000)
                sizeStr = size / 1000 + " KB";
            else if (size < 1000000000)
                sizeStr = size / 1000000 + " MB";
            else
                sizeStr = size / 1000000000 + " GB";

            item.SubItems.Add(sizeStr);

            listView1.Items.Add(item);
        }



        // 给 listView 绑定右键菜单
        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ListViewItem item = listView1.GetItemAt(e.X, e.Y);
                Console.WriteLine($"item: {item}");

                // 根据当前的显示模式，设置菜单项的状态
                View view = listView1.View;
                menuViewDetail.Checked = (view == View.Details);
                menuViewList.Checked = (view == View.List);
                menuViewLargeIcon.Checked = (view == View.LargeIcon);
                menuViewSmallIcon.Checked = (view == View.SmallIcon);

                contextMenuStrip1.Show(listView1, e.Location);
            }
        }

        private void menuViewDetail_Click(object sender, EventArgs e)
        {
            listView1.View = View.Details;
        }

        private void menuViewList_Click(object sender, EventArgs e)
        {
            listView1.View = View.List;
        }

        private void menuViewLargeIcon_Click(object sender, EventArgs e)
        {
            listView1.View = View.LargeIcon;
        }

        private void menuViewSmallIcon_Click(object sender, EventArgs e)
        {
            listView1.View = View.SmallIcon;
        }

        // ListView 单列点击事件
        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == 0)
            {
                this.asc = !asc;
                if (asc)
                {
                    listView1.ListViewItemSorter = new MyListItemSorter(true);
                    listView1.Sort();
                    listView1.Columns[0].ImageKey = "Sort_ASC";
                }
                else
                {
                    listView1.ListViewItemSorter = new MyListItemSorter(false);
                    listView1.Sort();
                    listView1.Columns[0].ImageKey = "Sort_DESC";
                }
            }
        }
    }
}
